/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2022-2022. All rights reserved.
 */

/**
 * 一个用Map来保存图中的邻接信息的数据结构
 *
 * @since 2022/4/24
 */
export class MultiMap {
  private _nMap: Map<string, Set<string>> = new Map<string, Set<string>>();

  put(key: string, value?: string): void {
    let values = this._nMap.get(key);
    if (values === undefined) {
      values = new Set<string>();
      this._nMap.set(key, values);
    }
    if (value !== undefined) {
      values!.add(value);
    }
  }

  get(key: string): Set<string> {
    const values = this._nMap.get(key);
    return values === undefined ? new Set() : values;
  }

  keys(): string[] {
    return [...this._nMap.keys()];
  }

  removeAll(key: string): Set<string> {
    const values = this._nMap.get(key);
    this._nMap.delete(key);
    return values === undefined ? new Set() : values;
  }

  remove(key: string, value: string): void {
    const values = this._nMap.get(key);
    if (values !== undefined) {
      values.delete(value);
    }
  }

  clear(): void {
    this._nMap.clear();
  }
}
